package com.sangeng.service.impl;

import com.sangeng.domain.entity.user.LoginUser;
import com.sangeng.domain.entity.user.User;
import com.sangeng.mapper.MenuMapper;
import com.sangeng.service.IMenuService;
import com.sangeng.service.IUserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Resource
    private IUserService userService;
    @Resource
    private MenuMapper menuMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.selectByUserName(username);
        if(user == null){
            throw new RuntimeException("用户名或密码错误");//当输错信息后会提示这个错误信息
        }
        //根据用户查询权限信息,添加到LoginUser中
//        List<String> list = new ArrayList<>(Arrays.asList("test","admin"));
        List<String> list = menuMapper.selectPermsByUserId(user.getId());
        //封装成LoginUser对象返回
        return new LoginUser(user,list);
    }
}
